package com.luhuiguo.gwt.security.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.luhuiguo.gwt.security.exception.GWTSecurityException;

/**
 * 
 * @author LuHuiguo
 * @version $Id: SecuredAsyncCallback.java 4 2009-12-16 08:14:27Z luhuiguo $
 * @param <T>
 */
public abstract class SecuredAsyncCallback<T> implements AsyncCallback<T> {

    /**
     * Override this method to implement your security errors management.
     * 
     * @param exception
     *            the security exception thrown
     */
    protected abstract void onSecurityException(final GWTSecurityException exception);

    /**
     * Acts as the onFailure in a classic AsyncCallback class.
     * 
     * @param exception
     *            the exception thrown
     */
    protected abstract void onOtherException(final Throwable exception);

    /**
     * Override (and protect) the onFailure method in order to provide a
     * security exception detection.
     * 
     * @param exception
     *            the exception thrown, could be a security exception
     */
    public final void onFailure(final Throwable exception) {
        if (exception != null && exception instanceof GWTSecurityException) {
            onSecurityException((GWTSecurityException) exception);
        } else {
            onOtherException(exception);
        }
    }

}
